package com.yx.config.saTokenConfig;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpInterface;

import com.yx.common.core.constant.Constants;
import com.yx.properties.PermissionProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


/**
 *
 */
@Component
public class StpInterfaceImpl implements StpInterface {



    @Resource
    private PermissionProperties permissionProperties;



    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String permission = SaHolder.getRequest().getRequestPath().substring(1).replaceAll("/", ".");

        //获取当前登录用户的权限集
        List<String> loginUserPermissionList = SaSessionCustomUtil.getSessionById(Constants.CUSTOM_SESSION_PREFIX+loginId).get(SaSession.PERMISSION_LIST, () -> {
            return permissionProperties.getForEntity(loginId);
        });
        //获取全量权限集
        List<String> permissionsList = SaSessionCustomUtil.getSessionById(Constants.CUSTOM_SESSION_PERMISSION_ALL).get(SaSession.PERMISSION_LIST, () -> {
            return permissionProperties.getForEntity(null);
        });
        //权限表未管控的情况下放行该接口权限
        if(!permissionsList.contains(permission)){
            loginUserPermissionList.add(permission);
        }
        return loginUserPermissionList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        return null;
    }
}
